//
//  EditDateController.m
//  iStage
//
//  Created by michael on 26/07/2008.
//  Copyright 2008 Legal Education Technology Lab. All rights reserved.
//

#import "EditDateController.h"
#import "DataManager.h"

@implementation EditDateController
@synthesize dateDisplay, datePicker,ValueKey;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
		dateDisplay = [[UITextField alloc] initWithFrame:CGRectZero];
		datePicker.locale=nil;
	}
	return self;
}

-(IBAction)cancelAction {
	[datePicker resignFirstResponder];
	NSLog(@"Dismissing date edit controller");
	[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)updateAction{
	NSLog(@"Updating Date");
	DataManager *dm = [DataManager sharedDataManager];
	[[dm currentProduction] setObject:datePicker.date forKey:ValueKey];
	[self cancelAction];

}
/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */

/*
 If you need to do additional setup after loading the view, override viewDidLoad.
- (void)viewDidLoad {
	
}
*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return YES;
//	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}

-(IBAction)UpdateDateDisplay {
	NSLog(@"Refreshing date display");
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	[df setDateStyle:NSDateFormatterMediumStyle];
	
	dateDisplay.text= [df stringFromDate:datePicker.date];
	[df release];
}
-(void)viewDidAppear:(BOOL)animated {

}
-(void)viewWillAppear:(BOOL)animated {
	
	DataManager *dm = [DataManager sharedDataManager];
	
	id obj = [dm.currentProduction objectForKey:ValueKey];
	NSDate *datevalue = [NSDate date];
	
	if (obj != nil) {
		datevalue = obj;
	}
	if (obj == @"") {
		datevalue = nil;
		
	}
	if (datevalue ==nil) {
		datevalue = [NSDate date];
	}
	if (datevalue!= datePicker.date) {
		NSLog(ValueKey);
		NSDateFormatter *df = [[NSDateFormatter alloc] init];
		[df setDateStyle:NSDateFormatterMediumStyle];
		
		NSLog([df stringFromDate:datevalue]);
		datePicker.date = datevalue;
		NSLog(@"Completed setting date");
		[self UpdateDateDisplay];
		[df release];
	}
	
}

@end
